package com.junmeng.demo.libtopview

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.junmeng.demo.libtopview.databinding.SimpleViewFactoryDialogBinding
import com.junmeng.lib.ui.topview.core.IViewFactory
import com.junmeng.lib.ui.topview.core.WindowTopViewManager

/**
 * 实现类似对话框效果
 */
class SimpleDialogViewFactory : IViewFactory {
    companion object {
        const val TAG = "SimpleViewFactory"
        const val VIEW_TYPE_SIMPLE_DIALOG = "view_type_simple_dialog"
    }

    private lateinit var mBinding: SimpleViewFactoryDialogBinding

    override fun getType(): String {
        return VIEW_TYPE_SIMPLE_DIALOG
    }

    override fun onCreateView(parent: ViewGroup, inflater: LayoutInflater): View {
        Log.i(TAG, "onCreateView")
        mBinding = SimpleViewFactoryDialogBinding.inflate(inflater, parent, false)
        mBinding.btnPositive.setOnClickListener {

        }
        mBinding.btnNeutral.setOnClickListener {

        }
        mBinding.btnNegative.setOnClickListener {
            WindowTopViewManager.unapplyViewFactory(getType())
        }

        mBinding.btnClose.setOnClickListener{
            WindowTopViewManager.unapplyViewFactory(getType())
        }

        return mBinding.root
    }

    override fun onBackPressed(): Boolean {
        Log.i(TAG, "onBackPressed")
        if (mBinding.root.parent != null
            && mBinding.root.isShown
            && mBinding.root.windowVisibility == View.VISIBLE
        ) {
            WindowTopViewManager.unapplyViewFactory(getType())
            return true
        }
        return super.onBackPressed()
    }

    override fun onAddIntoContainerView(parent: ViewGroup, child: View) {
        Log.i(TAG, "onAddIntoContainerView")
        super.onAddIntoContainerView(parent, child)
    }

    override fun onRemoveFromContainerView(parent: ViewGroup, child: View) {
        Log.i(TAG, "onRemoveFromContainerView")
        super.onRemoveFromContainerView(parent, child)
    }

    override fun onDestroyView() {
        Log.i(TAG, "onDestroyView")
        super.onDestroyView()
    }


    override fun onlyApplyCurrent(): Boolean {
        return true
    }
}